package org.dong.samples.dao.Impl;

import java.util.List;

import org.dong.samples.dao.RoleHibernateDAO;
import org.dong.samples.model.UserRole;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional
public class RoleHibernateDAOImpl extends BaseHibernateDAO<UserRole> implements RoleHibernateDAO  {
	
	@Autowired
	private SessionFactory sessionFactory;
	
	public Session getSession(){
		return this.sessionFactory.getCurrentSession();
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<UserRole> findAllUserRole() {
		Session session = getSession();
		String hql = "FROM UserRole";
		List<UserRole> roleList= session.createQuery(hql).list();
		return roleList;
	}

	@SuppressWarnings("unchecked")
	@Override
	public UserRole getRoleByUser(String userId) {
		Session session = getSession();
		List<UserRole> role = session.createQuery("from UserRole where userId="+userId).list();
		for (UserRole ur : role) {
			if(ur.getRoleId()==3 || ur.getRoleId()==2 || ur.getRoleId()==1){
				return ur;
			}
		}
		return null;
	}
}
